/*********************************************************************************
 *      Copyright:  (C) 2022 Zhang xiangyun<1327295677@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  signal.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(02/05/02)
 *         Author:  Zhang xiangyun <1327295677@qq.com>
 *      ChangeLog:  1, Release initial version on "02/05/22 21:24:38"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <execinfo.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>

int g_stop;
int install_signal(void);
void signal_stop(int signum);
void signal_code(int signum);

void signal_stop(int signum)/* get SIGTERM signal to kill program */
{
	if(SIGTERM == signum)
	{
		printf("SIGTERM signal detected,start to kill this program...\n");
		g_stop=1;
	}
}

void signal_code(int signum)/*get code error signal to exit program*/
{
	if(SIGBUS == signum)
	{
		printf("SIGBUS signal detected,start to exit ...\n");
	}
	else if(SIGILL == signum)
	{
		printf("SIGILL signal detected, start to exit...\n");
	}
	else if(SIGSEGV == signum)
	{
    	printf("SIGSEGV signal detected,start to exit...\n");
	}
	exit(-1);
}

int install_signal(void)
{

	g_stop=0;
	struct sigaction sigstop,sigcode,sigign;
          /*set sigaction*/
	sigemptyset(&sigstop.sa_mask);
	sigstop.sa_flags=0;
	sigstop.sa_handler=signal_stop;

	sigemptyset(&sigcode.sa_mask);
	sigcode.sa_flags=0;
	sigcode.sa_handler=signal_code;

	sigemptyset(&sigign.sa_mask);
	sigign.sa_flags=0;
	sigign.sa_handler=SIG_IGN;

	sigaction(SIGTERM,&sigstop,0);
	sigaction(SIGBUS,&sigcode,0);
	sigaction(SIGILL,&sigcode,0);
	sigaction(SIGSEGV,&sigcode,0);

	return 0;
}

